package com.bodgittandscarper.broker.utils;

import static java.util.logging.Level.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * This class encapsulates handling of the property file <code>suncertify.properties</code>.
 * If the property file does not exist, it will be created in working directory.
 * If it exist, the property file will be read, and its properties will be accessible. 
 * This is done when the class is loaded.
 * 
 * @author Morten Rummelhoff
 * @see java.util.Properties
 */
public class PropertyHandler {

	private Logger log = Logger.getLogger(getClass().getName());

	/**
	 * Property file named suncertify.properties. This is assumed to be located in working directory. 
	 */
	public static final String PROPERTY_FILE_NAME = "suncertify.properties";
	
	//singleton implementation
	private static final PropertyHandler instance = new PropertyHandler();
	
	private File propertyFile;
	private Properties properties;	
	
	//private constructor for unintentionally instantiation. 
	private PropertyHandler() {
		initialize();
	}
	
	private void initialize() {

		//first initialize property file
		properties = new Properties();
		propertyFile = new File(PropertyHandler.PROPERTY_FILE_NAME);
		
		
		//always try to createNewFile, but only if it does not exist. Ignore the return value.
		try {
			propertyFile.createNewFile();
			
			InputStream inputStream = new FileInputStream(propertyFile);
			properties.load(inputStream);
			inputStream.close();
			
		} catch (IOException e) {
			if (log.isLoggable(SEVERE)) {
				log.log(SEVERE, "Unable to initialize propertyfile.", e);
			}
		}
	}
	/**
	 * Singleton method to access <code>PropertyHandler</code>. Only one instance of this class can exist per JVM.  
	 * 
	 * @return A reference to the <code>PropertyHandler</code> 
	 */
	public static PropertyHandler getInstance() {
		return instance;
	}
	
	/**
	 * Searches the property with the specified key. If it does not exist, null is returned.
	 * <p>
	 * Method simply calls <code>getProperty(String)</code> in the <code>Properties</code> class.
	 * 
	 * @param key the property key
	 * @return the property value for the specified key.
	 */
	public String getProperty(String key) {
		return properties.getProperty(key);
	}
	
	/**
	 * Sets a property with the specified key and value. If the property already exist, it is overwritten.
	 * Only String objects can be used for defining a property.
	 * <p>
	 * Method simply calls <code>setProperty(String, String)</code> in the <code>Properties</code> class.
	 * 
	 * @param key the key identifying the name of the property to be set.
	 * @param value the value for property to be set.
	 */
	public void setProperty(String key, String value) {
		properties.setProperty(key, value);
		
		if (log.isLoggable(INFO))
			log.log(INFO, key + ": " + properties.getProperty(key));

	}
	
	/**
	 * Writes the properties to the defined suncertyfy.properties file. It uses <code>store</code> method in the 
	 * <code>Properties</code> class. 
	 */
	public void storeProperty() {
		
		try {
			OutputStream outputStream = new FileOutputStream(propertyFile); 
			properties.store(outputStream, "Store DB path");
			outputStream.close();			
		} catch (FileNotFoundException e) {
			if (log.isLoggable(WARNING)) {
				log.log(WARNING, "Unable to store propertyfile.", e);
			}
		} catch (IOException e) {
			if (log.isLoggable(WARNING)) {
				log.log(WARNING, "Unable to initialize propertyfile.", e);
			}
		}
		
	}

	
}
